﻿namespace HuaWei.DAL.SqlClient
{
    using HuaWei;
    using HuaWei.DAI;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlServerCe;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    public class CMSSQLCeDataAccess : IDataAccess
    {
        private ConcurrentProcessType concurrentProcessType = ConcurrentProcessType.MANUAL_UPDATE;
        private bool flag = false;
        private GInterface gnumerface = new MSSqlCeConcurrentProcessor();
        private const int num = 0;
        private SqlCeConnection sqlCeConnection = null;
        private SqlCeTransaction sqlCeTransaction = null;
        private string str;
        private string str1;

        private string str2;

        private string str3;

        public void BeginTrans()
        {
            try
            {
                this.sqlCeTransaction = this.sqlCeConnection.BeginTransaction();
                this.flag = true;
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
        }

        public void CloseDatabase()
        {
            try
            {
                this.sqlCeConnection.Close();
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
        }

        public void Commit()
        {
            try
            {
                this.sqlCeTransaction.Commit();
                this.flag = false;
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
        }

        public DataSet ExecuteDataSet(string exeStr, CommandType cmdType)
        {
            DataSet set;
            try
            {
                set = this.ExecuteDataSet(exeStr, cmdType, null, null, null);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return set;
        }

        public DataSet ExecuteDataSet(string exeStr, CommandType cmdType, string TableName)
        {
            DataSet set;
            try
            {
                set = this.ExecuteDataSet(exeStr, cmdType, null, null, TableName);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return set;
        }

        public DataSet ExecuteDataSet(string exeStr, CommandType cmdType, DbParameter[] inParams)
        {
            DataSet set;
            try
            {
                set = this.ExecuteDataSet(exeStr, cmdType, inParams, null, null);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return set;
        }

        public DataSet ExecuteDataSet(string exeStr, DbParameter[] outParams, string TableName)
        {
            DataSet set;
            try
            {
                set = this.ExecuteDataSet(exeStr, CommandType.StoredProcedure, null, outParams, TableName);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return set;
        }

        public DataSet ExecuteDataSet(string exeStr, CommandType cmdType, DbParameter[] inParams, string TableName)
        {
            DataSet set;
            try
            {
                set = this.ExecuteDataSet(exeStr, cmdType, inParams, null, TableName);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return set;
        }

        public DataSet ExecuteDataSet(string exeStr, CommandType cmdType, DbParameter[] inParams, DbParameter[] outParams)
        {
            DataSet set;
            try
            {
                set = this.ExecuteDataSet(exeStr, cmdType, inParams, outParams, null);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return set;
        }

        public DataSet ExecuteDataSet(string exeStr, CommandType cmdType, DbParameter[] inParams, DbParameter[] outParams, string TableName)
        {
            DataSet set2;
            try
            {
                int num;
                SqlCeParameter parameter;
                DataSet dataSet = new DataSet();
                SqlCeDataAdapter adapter = new SqlCeDataAdapter();
                this.OpenDatabase();
                SqlCeCommand command = new SqlCeCommand(exeStr, this.sqlCeConnection);
                command.CommandType = cmdType;
                command.CommandTimeout = 0;
                if (this.flag)
                {
                    command.Transaction = this.sqlCeTransaction;
                }
                if ((inParams != null) && (inParams.Length > 0))
                {
                    for (num = 0; num < inParams.Length; num++)
                    {
                        parameter = new SqlCeParameter(inParams[num].ParameterName, inParams[num].Value);
                        parameter.Direction = ParameterDirection.Input;
                        command.Parameters.Add(parameter);
                    }
                }
                if (((cmdType == CommandType.StoredProcedure) && (outParams != null)) && (outParams.Length > 0))
                {
                    for (num = 0; num < outParams.Length; num++)
                    {
                        parameter = new SqlCeParameter(outParams[num].ParameterName, SqlDataTypeConvert.GetSqlDbType(outParams[num].DbType), outParams[num].Size);
                        parameter.Direction = ParameterDirection.Output;
                        command.Parameters.Add(parameter);
                    }
                }
                adapter.SelectCommand = command;
                if ((TableName == null) || TableName.Equals(""))
                {
                    adapter.Fill(dataSet);
                }
                else
                {
                    adapter.Fill(dataSet, TableName);
                }
                if (((cmdType == CommandType.StoredProcedure) && (outParams != null)) && (outParams.Length > 0))
                {
                    for (num = 0; num < outParams.Length; num++)
                    {
                        outParams[num].Value = command.Parameters[outParams[num].ParameterName].Value;
                    }
                }
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                Log.Write("ExecuteDataSet Error Message:" + exception.Message + " SQL:" + exeStr);
                throw exception;
            }
            return set2;
        }

        public bool ExecuteNonQuery(ArrayList sqls, CommandType cmdType)
        {
            try
            {
                foreach (string str in sqls)
                {
                    if (!string.IsNullOrEmpty(str))
                    {
                        this.ExecuteNonQuery(str, cmdType, null, null);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool ExecuteNonQuery(Hashtable sqls, CommandType cmdType)
        {
            throw new NotImplementedException();
        }

        public int ExecuteNonQuery(string exeStr, CommandType cmdType, DbParameter[] inParams, DbParameter[] outParams)
        {
            int num3;
            try
            {
                int num;
                SqlCeParameter parameter;
                this.OpenDatabase();
                SqlCeCommand command = new SqlCeCommand(exeStr, this.sqlCeConnection);
                command.CommandType = cmdType;
                command.CommandTimeout = 0;
                if (this.flag)
                {
                    command.Transaction = this.sqlCeTransaction;
                }
                if ((inParams != null) && (inParams.Length > 0))
                {
                    for (num = 0; num < inParams.Length; num++)
                    {
                        parameter = new SqlCeParameter(inParams[num].ParameterName, SqlDataTypeConvert.GetSqlDbType(inParams[num].DbType));
                        parameter.Value = inParams[num].Value;
                        parameter.Direction = ParameterDirection.Input;
                        command.Parameters.Add(parameter);
                    }
                }
                if (((cmdType == CommandType.StoredProcedure) && (outParams != null)) && (outParams.Length > 0))
                {
                    for (num = 0; num < outParams.Length; num++)
                    {
                        parameter = new SqlCeParameter(outParams[num].ParameterName, SqlDataTypeConvert.GetSqlDbType(outParams[num].DbType), outParams[num].Size);
                        parameter.Direction = ParameterDirection.Output;
                        command.Parameters.Add(parameter);
                    }
                }
                int num2 = command.ExecuteNonQuery();
                if (((cmdType == CommandType.StoredProcedure) && (outParams != null)) && (outParams.Length > 0))
                {
                    for (num = 0; num < outParams.Length; num++)
                    {
                        outParams[num].Value = command.Parameters[outParams[num].ParameterName].Value;
                    }
                }
                num3 = num2;
            }
            catch (Exception exception)
            {
                Log.Write("Error Message:" + exception.Message + " SQL:" + exeStr);
                throw exception;
            }
            return num3;
        }

        public object ExecuteReader(string exeStr, CommandType cmdType, DbParameter[] inParams, DbParameter[] outParams)
        {
            object obj3;
            try
            {
                int num;
                SqlCeParameter parameter;
                this.OpenDatabase();
                SqlCeCommand command = new SqlCeCommand(exeStr, this.sqlCeConnection);
                command.CommandType = cmdType;
                command.CommandTimeout = 0;
                if (this.flag)
                {
                    command.Transaction = this.sqlCeTransaction;
                }
                if ((inParams != null) && (inParams.Length > 0))
                {
                    for (num = 0; num < inParams.Length; num++)
                    {
                        parameter = new SqlCeParameter(inParams[num].ParameterName, inParams[num].Value);
                        parameter.Direction = ParameterDirection.Input;
                        command.Parameters.Add(parameter);
                    }
                }
                if (((cmdType == CommandType.StoredProcedure) && (outParams != null)) && (outParams.Length > 0))
                {
                    for (num = 0; num < outParams.Length; num++)
                    {
                        parameter = new SqlCeParameter(outParams[num].ParameterName, SqlDataTypeConvert.GetSqlDbType(outParams[num].DbType), outParams[num].Size);
                        parameter.Direction = ParameterDirection.Output;
                        command.Parameters.Add(parameter);
                    }
                }
                object obj2 = command.ExecuteReader();
                if (((cmdType == CommandType.StoredProcedure) && (outParams != null)) && (outParams.Length > 0))
                {
                    for (num = 0; num < outParams.Length; num++)
                    {
                        outParams[num].Value = command.Parameters[outParams[num].ParameterName].Value;
                    }
                }
                obj3 = obj2;
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return obj3;
        }

        public object ExecuteScalar(string exeStr, CommandType cmdType, DbParameter[] inParams, DbParameter[] outParams)
        {
            object obj3;
            try
            {
                int num;
                SqlCeParameter parameter;
                this.OpenDatabase();
                SqlCeCommand command = new SqlCeCommand(exeStr, this.sqlCeConnection);
                command.CommandType = cmdType;
                command.CommandTimeout = 0;
                if (this.flag)
                {
                    command.Transaction = this.sqlCeTransaction;
                }
                if ((inParams != null) && (inParams.Length > 0))
                {
                    for (num = 0; num < inParams.Length; num++)
                    {
                        parameter = new SqlCeParameter(inParams[num].ParameterName, inParams[num].Value);
                        parameter.Direction = ParameterDirection.Input;
                        command.Parameters.Add(parameter);
                    }
                }
                if (((cmdType == CommandType.StoredProcedure) && (outParams != null)) && (outParams.Length > 0))
                {
                    for (num = 0; num < outParams.Length; num++)
                    {
                        parameter = new SqlCeParameter(outParams[num].ParameterName, SqlDataTypeConvert.GetSqlDbType(outParams[num].DbType), outParams[num].Size);
                        parameter.Direction = ParameterDirection.Output;
                        command.Parameters.Add(parameter);
                    }
                }
                object obj2 = command.ExecuteScalar();
                if (((cmdType == CommandType.StoredProcedure) && (outParams != null)) && (outParams.Length > 0))
                {
                    for (num = 0; num < outParams.Length; num++)
                    {
                        outParams[num].Value = command.Parameters[outParams[num].ParameterName].Value;
                    }
                }
                obj3 = obj2;
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return obj3;
        }

        public DataSet GetDs(DataSet dsSource, string invokeCode, string param)
        {
            return null;
        }

        DataSet IDataAccess.ExecuteDataSet(string exeStr, DbParameter[] outParams)
        {
            DataSet set;
            try
            {
                set = this.ExecuteDataSet(exeStr, CommandType.StoredProcedure, null, outParams, null);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return set;
        }

        int IDataAccess.ExecuteNonQuery(string exeStr, CommandType cmdType)
        {
            int num;
            try
            {
                num = this.ExecuteNonQuery(exeStr, cmdType, null, null);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return num;
        }

        int IDataAccess.ExecuteNonQuery(string proName, DbParameter[] outParams)
        {
            int num;
            try
            {
                num = this.ExecuteNonQuery(proName, CommandType.StoredProcedure, null, outParams);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return num;
        }

        int IDataAccess.ExecuteNonQuery(string exeStr, CommandType cmdType, DbParameter[] inParams)
        {
            int num;
            try
            {
                num = this.ExecuteNonQuery(exeStr, cmdType, inParams, null);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return num;
        }

        object IDataAccess.ExecuteReader(string exeStr, CommandType cmdType)
        {
            object obj2;
            try
            {
                obj2 = this.ExecuteReader(exeStr, cmdType, null, null);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return obj2;
        }

        object IDataAccess.ExecuteReader(string exeStr, DbParameter[] outParams)
        {
            object obj2;
            try
            {
                obj2 = this.ExecuteReader(exeStr, CommandType.StoredProcedure, null, outParams);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return obj2;
        }

        object IDataAccess.ExecuteReader(string exeStr, CommandType cmdType, DbParameter[] inParams)
        {
            object obj2;
            try
            {
                obj2 = this.ExecuteReader(exeStr, cmdType, inParams, null);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return obj2;
        }

        object IDataAccess.ExecuteScalar(string exeStr, CommandType cmdType)
        {
            object obj2;
            try
            {
                obj2 = this.ExecuteScalar(exeStr, cmdType, null, null);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return obj2;
        }

        object IDataAccess.ExecuteScalar(string exeStr, DbParameter[] outParams)
        {
            object obj2;
            try
            {
                obj2 = this.ExecuteScalar(exeStr, CommandType.StoredProcedure, null, outParams);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return obj2;
        }

        object IDataAccess.ExecuteScalar(string exeStr, CommandType cmdType, DbParameter[] inParams)
        {
            object obj2;
            try
            {
                obj2 = this.ExecuteScalar(exeStr, cmdType, inParams, null);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
            return obj2;
        }

        public string Invoke(DataSet dsSource, string invokeCode, string param)
        {
            return "";
        }

        private void method(DataSet dataSet, DataSet dataSet1)
        {
            bool flag = false;
            bool flag2 = false;
            bool flag3 = false;
            DataSet changes = dataSet.GetChanges();
            if (changes.Tables.Count > 0)
            {
                DataTable table = dataSet1.Tables["ProcData"];
                DataRow row = null;
                string tableName = "";
                foreach (DataTable table2 in changes.Tables)
                {
                    tableName = table2.TableName;
                    flag = table2.Columns.Contains("xmid");
                    flag2 = table2.Columns.Contains("bdid");
                    flag3 = table2.Columns.Contains("qcid");
                    foreach (DataRow row2 in table2.Rows)
                    {
                        row = table.NewRow();
                        row["id"] = Guid.NewGuid().ToString();
                        row["BM"] = tableName;
                        row["tag"] = 0;
                        switch (row2.RowState)
                        {
                            case DataRowState.Detached:
                                row["RecId"] = row2["id"];
                                row["OperateType"] = 4;
                                if (flag)
                                {
                                    row["xmid"] = row2["xmid"];
                                }
                                if (flag2)
                                {
                                    row["bdid"] = row2["bdid"];
                                }
                                if (flag3)
                                {
                                    row["qcid"] = row2["qcid"];
                                }
                                break;

                            case DataRowState.Added:
                                row["RecId"] = row2["id"];
                                row["OperateType"] = 1;
                                if (flag)
                                {
                                    row["xmid"] = row2["xmid"];
                                }
                                if (flag2)
                                {
                                    row["bdid"] = row2["bdid"];
                                }
                                if (flag3)
                                {
                                    row["qcid"] = row2["qcid"];
                                }
                                break;

                            case DataRowState.Deleted:
                                row["RecId"] = row2["id", DataRowVersion.Original];
                                row["OperateType"] = 3;
                                if (flag)
                                {
                                    row["xmid"] = row2["xmid", DataRowVersion.Original];
                                }
                                if (flag2)
                                {
                                    row["bdid"] = row2["bdid", DataRowVersion.Original];
                                }
                                if (flag3)
                                {
                                    row["qcid"] = row2["qcid", DataRowVersion.Original];
                                }
                                break;

                            case DataRowState.Modified:
                                row["RecId"] = row2["id"];
                                row["OperateType"] = 2;
                                if (flag)
                                {
                                    row["xmid"] = row2["xmid"];
                                }
                                if (flag2)
                                {
                                    row["bdid"] = row2["bdid"];
                                }
                                if (flag3)
                                {
                                    row["qcid"] = row2["qcid"];
                                }
                                break;
                        }
                        row.EndEdit();
                        table.Rows.Add(row);
                    }
                }
            }
        }

        private void method1(object sender, SqlCeRowUpdatedEventArgs e)
        {
            if (this.gnumerface != null)
            {
                this.gnumerface.Process(e);
            }
        }

        public void OpenDatabase()
        {
            try
            {
                if (this.sqlCeConnection.State == ConnectionState.Closed)
                {
                    this.sqlCeConnection.Open();
                }
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
        }

        public void RollBack()
        {
            try
            {
                this.sqlCeTransaction.Rollback();
                this.flag = false;
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                throw exception;
            }
        }

        public string SyncData(DataSet dsSource)
        {
            return "";
        }

        public void Update(DataTable table)
        {
            try
            {
                SqlCeDataAdapter adapter = null;
                string selectCommandText = "Select ";
                foreach (DataColumn column in table.Columns)
                {
                    selectCommandText = selectCommandText + "[" + column.ColumnName.Replace("]", "]]") + "],";
                }
                selectCommandText = selectCommandText.Substring(0, selectCommandText.LastIndexOf(",")) + " from  [" + table.TableName.Replace("]", "]]") + "]";
                this.OpenDatabase();
                adapter = new SqlCeDataAdapter(selectCommandText, this.sqlCeConnection);
                if (this.flag)
                {
                    adapter.SelectCommand.Transaction = this.sqlCeTransaction;
                }
                SqlCeCommandBuilder builder = new SqlCeCommandBuilder(adapter);
                builder.QuotePrefix = "[";
                builder.QuoteSuffix = "]";
                if (this.flag)
                {
                    adapter.SelectCommand.Transaction = this.sqlCeTransaction;
                }
                this.gnumerface.ProcessType = this.concurrentProcessType;
                adapter.RowUpdated += new SqlCeRowUpdatedEventHandler(this.method1);
                adapter.Update(table);
            }
            catch (Exception exception)
            {
                Log.Write("Update Error Message:" + exception.Message);
                throw exception;
            }
        }

        public void Update(DataSet dsUpdate, [Optional, DefaultParameterValue(true)] bool writeLog)
        {
            if ((dsUpdate.Tables != null) && (dsUpdate.Tables.Count > 0))
            {
                if (writeLog)
                {
                    string exeStr = "select * from DelInfo where 1=2";
                    dsUpdate.Merge(this.ExecuteDataSet(exeStr, CommandType.Text, "DelInfo"));
                    Class9.smethod(dsUpdate);
                }
                foreach (DataTable table in dsUpdate.Tables)
                {
                    this.Update(table, table.TableName);
                }
            }
        }

        public void Update(DataSet dsUpdate, string strTableName)
        {
            DataSet set = new DataSet();
            string exeStr = string.Format("select * from {0} where 1>2", strTableName);
            set.Merge(this.ExecuteDataSet(exeStr, CommandType.Text, strTableName));
            DataTable table = dsUpdate.Tables[strTableName];
            for (int i = table.Columns.Count - 1; i >= 0; i--)
            {
                string columnName = table.Columns[i].ColumnName;
                if (!set.Tables[strTableName].Columns.Contains(columnName))
                {
                    table.Columns.Remove(columnName);
                }
            }
            this.Update(table);
        }

        public void Update(DataTable table, string tableName)
        {
            try
            {
                DataSet set = new DataSet();
                string exeStr = string.Format("select * from {0} where 1>2", tableName);
                set.Merge(this.ExecuteDataSet(exeStr, CommandType.Text, tableName));
                for (int i = table.Columns.Count - 1; i >= 0; i--)
                {
                    string columnName = table.Columns[i].ColumnName;
                    if (!set.Tables[tableName].Columns.Contains(columnName))
                    {
                        table.Columns.Remove(columnName);
                    }
                }
            }
            catch
            {
            }
            this.Update(table);
        }

        public void Update(DataSet dsChanges, Dictionary<DataRowState, string[]> updateIndex, params string[] exeStr)
        {
            int i;
            string tableName;
            string str;
            string[] value;
            int k;
            this.OpenDatabase();
            this.BeginTrans();
            try
            {
                try
                {
                    DataSet dataSet = new DataSet();
                    for (i = dsChanges.Tables.Count - 1; i >= 0; i--)
                    {
                        tableName = dsChanges.Tables[i].TableName;
                        str = string.Format("select * from {0} where 1>2", tableName);
                        dataSet.Merge(this.ExecuteDataSet(str, CommandType.Text, tableName));
                    }
                    for (i = dsChanges.Tables.Count - 1; i >= 0; i--)
                    {
                        tableName = dsChanges.Tables[i].TableName;
                        for (int j = dsChanges.Tables[i].Columns.Count - 1; j >= 0; j--)
                        {
                            string columnName = dsChanges.Tables[i].Columns[j].ColumnName;
                            if (!dataSet.Tables[tableName].Columns.Contains(columnName))
                            {
                                dsChanges.Tables[i].Columns.Remove(columnName);
                            }
                        }
                    }
                    foreach (KeyValuePair<DataRowState, string[]> keyValuePair in updateIndex)
                    {
                        DataSet changes = dsChanges.GetChanges(keyValuePair.Key);
                        if (changes == null)
                        {
                            continue;
                        }
                        value = keyValuePair.Value;
                        for (k = 0; k < (int)value.Length; k++)
                        {
                            tableName = value[k];
                            if (changes.Tables.Contains(tableName))
                            {
                                this.Update(changes, tableName);
                            }
                        }
                    }
                    value = exeStr;
                    for (k = 0; k < (int)value.Length; k++)
                    {
                        str = value[k];
                        if (!string.IsNullOrEmpty(str))
                        {
                            this.ExecuteNonQuery(str, CommandType.Text, null, null);
                        }
                    }
                    this.Commit();
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    Log.Write(exception.Message);
                    this.RollBack();
                    throw exception;
                }
            }
            finally
            {
                this.CloseDatabase();
            }
        }

        public ConcurrentProcessType CCProcessType
        {
            get
            {
                return this.concurrentProcessType;
            }
            set
            {
                this.concurrentProcessType = value;
            }
        }

        public string ConnectionString
        {
            get
            {
                return this.str1;
            }
            set
            {
                this.str1 = value;
                this.sqlCeConnection = new SqlCeConnection(this.str1);
            }
        }

        public ConnectionState ConnState
        {
            get
            {
                return this.sqlCeConnection.State;
            }
        }

        public string DBType
        {
            get
            {
                return this.str;
            }
            set
            {
                this.str = value;
            }
        }

        public string ProductNo
        {
    
            get
            {
                return this.str3;
            }
    
            set
            {
                this.str3 = value;
            }
        }

        public string ServerDBType
        {
    
            get
            {
                return this.str2;
            }
    
            set
            {
                this.str2 = value;
            }
        }
    }
}

